/*
 * Copyright 2011-2020 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0.
 * See `LICENSE` in the project root for license information.
 */

package me.ijleex.mgmt.system.menu.action;

import java.util.List;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import me.ijleex.mgmt.commons.util.MathUtils;
import me.ijleex.mgmt.framework.entity.Result;
import me.ijleex.mgmt.framework.page.Page;
import me.ijleex.mgmt.framework.spring.ControllerSupport;
import me.ijleex.mgmt.system.menu.entity.Menu;
import me.ijleex.mgmt.system.menu.service.IMenuService;

/**
 * 菜单 信息操作处理
 *
 * @author liym
 * @since 2019-03-14 12:28:19 By 'codegen'
 */
@RequestMapping("/system/menu")
@Controller
public class MenuAction extends ControllerSupport<Menu> {

    /**
     * HTML 路径前缀
     */
    private static final String PATH_PREFIX = "system/menu";

    private final IMenuService service;

    public MenuAction(IMenuService service) {
        this.service = service;
    }

    @GetMapping("/")
    public String index() {
        return PATH_PREFIX + "/index";
    }

    /**
     * 新增 菜单
     */
    @GetMapping("/add")
    public String add() {
        return PATH_PREFIX + "/add";
    }

    /**
     * 新增 菜单
     */
    @PostMapping("/add")
    @ResponseBody
    public Result add(Menu menu) {
        return this.service.insertMenu(menu);
    }

    /**
     * 删除 菜单
     *
     * <p>curl -d "ids=526550660093693961" http://localhost:8082/ij-mgmt/system/menu/remove</p>
     */
    @PostMapping("/remove")
    @ResponseBody
    public Result remove(String ids) {
        return this.service.deleteMenuByIds(ids);
    }

    /**
     * 修改 菜单
     */
    @GetMapping("/update/{id}")
    public String update(@PathVariable("id") Long id, ModelMap mmap) {
        Menu menu = this.service.getMenuById(id);
        mmap.put("menu", menu);
        return PATH_PREFIX + "/update";
    }

    /**
     * 修改 菜单
     */
    @PostMapping("/update")
    @ResponseBody
    public Result update(Menu menu) {
        return this.service.updateMenu(menu);
    }

    /**
     * 查询 菜单列表
     *
     * <p>http://localhost:8082/ij-mgmt/system/menu/list?pageNo=1&pageSize=7</p>
     * <p>http://localhost:8082/ij-mgmt/system/menu/list?offset=0&limit=7</p>
     */
    @GetMapping("/list")
    @ResponseBody
    public Page<Menu> getMenuList(HttpServletRequest request, Menu menu) {
        this.setPageParams(request, menu);
        List<Menu> list = this.service.getMenuList(menu);
        return this.wrapPageData(list, menu);
    }

    /**
     * 获取用户的菜单（树）
     *
     * <p>http://localhost:8082/ij-mgmt/system/menu/tree/getByUser?systemId=ijleex-mgmt</p>
     *
     * @param request 请求
     * @param systemId 系统标识符
     * @return 用户菜单（树）
     * @version 2019-03-15 13:20
     * @since 2019-03-15 13:21
     */
    @GetMapping("/tree/getByUser")
    @ResponseBody
    public Result getMenuTreeByUser(HttpServletRequest request, String systemId) {
        // TODO 获取当前登录用户的 ID
        Long userId = 577054734542819328L;
        if (MathUtils.isNullOrZero(userId)) {
            return Result.error("用户未登录");
        }
        return this.service.getMenuTreeByUser(userId, systemId);
    }

    /**
     * 获取角色的菜单（树）
     *
     * <p>http://localhost:8082/ij-mgmt/system/menu/tree/getByRole?roleId=376597004234641409</p>
     *
     * @param request 请求
     * @param roleId 角色 ID
     * @return 菜单（树）
     * @version 2019-03-15 15:29
     * @since 2016-08-23 17:08:49
     */
    @GetMapping("/tree/getByRole")
    @ResponseBody
    public Result getMenuTreeByRole(HttpServletRequest request, Long roleId) {
        if (MathUtils.isNullOrZero(roleId)) {
            return Result.invalidArg("角色id不能为空：roleId");
        }
        return this.service.getMenuTreeByRole(roleId);
    }

    /**
     * 获取整个菜单树
     *
     * @param request 请求
     * @param object 菜单
     * @return 菜单树
     * @version 2019-03-15 15:32
     */
    @GetMapping("/tree")
    @ResponseBody
    public Result getMenuTree(HttpServletRequest request, Menu object) {
        return this.service.getMenuTree(object);
    }

}
